package com.itcast.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itcast.common.AjaxResult;
import com.itcast.common.BaseController;
import com.itcast.common.TableDataInfo;
import com.itcast.entity.User;
import com.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    // http://localhost:8100/list
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<User> list = userService.selectAllUsers();
        return getDataTable(list);
    }

    //  http://localhost:8100/1
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(userService.selectUserById(id));
    }

    //  http://localhost:8100/add
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    //  http://localhost:8100/update
    @PutMapping("/update")
    public AjaxResult update(@Validated @RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    //http://localhost:8100/delete/3
    @GetMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(userService.deleteUserById(id));
    }


}